package com.bc.trade.service;

import java.util.List;

import com.bc.common.PayType;
import com.alibaba.fastjson.JSON;
import com.bc.common.CommonEnum.Resp;
import com.bc.common.message.WxgjClearingReq;
import com.bc.common.message.WxgjClearingResp;
import com.bc.trade.channel.ChannelFactory;
import com.bc.trade.channel.IChannel;
import com.bc.trade.config.R;
import com.bc.trade.model.Merch;
import com.bc.trade.model.TradeOrder;
import com.bc.trade.model.TradeStrategy;
import com.bc.trade.model.WxClear;
import com.fs.common.util.FSDate;
import com.fs.common.util.FSLog;

public class WxSettlementQuery implements Runnable {
	@Override
	public void run() {
		int x = 1;
		
		try {
			Thread.sleep(1000*9);//
			//FSLog.weixin.info(FSDate.getDateTime() + ":" +Thread.currentThread().getId() +":" + "开始睡眠...");
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		do {
			FSLog.weixin.info(FSDate.getDateTime() + ":" +Thread.currentThread().getId() +":" + "定时查询微信清算状态任务开始执行……");

			TradeStrategy tsx = TradeStrategy.dao.findFirst("select * from bd_trade_strategy where channelCode=?",
					"wxgipay");
			if (tsx == null || tsx.getMerchCode() == null) {
				continue;
			}
			List<Merch> merchList = Merch.dao.find("select * from bd_merch");
			if (merchList == null || merchList.isEmpty()) {
				continue;
			}
			for (int t = 0; t < merchList.size(); t++) {
				WxClear wxclear = WxClear.dao.selectMaxEndDate(merchList.get(t).getCode());
				String start_date = null;
				String end_date = null;
				Merch merchCode = merchList.get(t);
				TradeOrder orderEnd = null;
				if (wxclear == null || wxclear.getDateEnd() == null) {
					// 查询此商户，第一笔成功的交易日期 和 此商户最后一笔成功的交易
					TradeOrder orderFirst = TradeOrder.dao.selecSuccessTradeOrderFirstDate(merchList.get(t).getCode());
					if (orderFirst == null || orderFirst.getSettleDate() == null) {
						continue;
					}
					if (FSDate.getDate().equals(orderFirst.getSettleDate())) {
						continue;
					}
					orderEnd = TradeOrder.dao.selecSuccessTradeOrderEndDate(merchList.get(t).getCode());
					if (orderEnd.getSettleDate().equals(orderFirst.getSettleDate())) {
						continue;
					}
					// 30天后的日期
					String tmp_date = FSDate.getPlusDays(orderFirst.getSettleDate(), 30).toString();
					if (orderEnd.getSettleDate().compareTo(tmp_date) > 0) {
						end_date = tmp_date; // orderEnd.getSettleDate().replace("-", "");
						end_date = end_date.replace("-", "");
					} else {
						end_date = orderEnd.getSettleDate().replace("-", "");
					}
					start_date = orderFirst.getSettleDate().replace("-", "");

				} else {
					if (FSDate.getDate().equals(wxclear.getDateSettlement())) {
						continue;
					}
					start_date = FSDate.getPlusDays(wxclear.getDateSettlement(), 1).toString();
					start_date = start_date.replace("-", "");

					orderEnd = TradeOrder.dao.selecSuccessTradeOrderEndDate(merchList.get(t).getCode());
					if (null == orderEnd || null == orderEnd.getSettleDate()) {
						continue;
					}
					end_date = orderEnd.getSettleDate().replace("-", "");

					if (end_date.compareTo(start_date) <= 0) {
						end_date = FSDate.getDate().replace("-", "");
					}

				}

				TradeStrategy ts = TradeStrategy.dao.selectFromRedis(orderEnd.getMerchCode(), orderEnd.getPayType());
				if (ts == null) {
					continue;
				}

				WxgjClearingReq req = new WxgjClearingReq();
				req.setVersion("V1.0");
				req.setMerchCode(merchCode.getCode());
				req.setOrgCode(orderEnd.getOrgCode());
				
				String current = FSDate.getDate();
				
				// 2天前的日期
				String tmp_date = FSDate.getPlusDays(current, -2).toString();
				
				req.setDate_start(tmp_date.replace("-", ""));
				req.setDate_end(current.replace("-", ""));
				req.setPayType(PayType.WXGJ_CLEARING_QCURE);

				WxgjClearingResp resp = new WxgjClearingResp(req);
				IChannel channel = ChannelFactory.get(orderEnd.getChannelCode());
				try {
					resp = channel.wxgjGetClearingData(req, ts);
				} catch (Exception e) {
					FSLog.weixin.error(FSDate.getDateTime() + ":" +Thread.currentThread().getId() +":微信国际清算信息查询:" + e);
					resp.setCodeAndMsg(Resp.Fail.getCode(), Resp.Fail.getMsg());
				} finally {
					try {
						Thread.sleep(1000*60*60*24);//第二天再执行
						FSLog.weixin.info(FSDate.getDateTime() + ":" +Thread.currentThread().getId() +":" + "开始睡眠...");
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		} while (x > 0);
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		WxSettlementQuery wx = new WxSettlementQuery();

		new Thread(wx).start();

	}

}
